package com.our.shop.system.base.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.our.shop.front.customer.web.CustomerController;
import com.our.shop.system.base.dao.IBaseDAO;
import com.our.shop.system.base.model.BaseObject;
import com.our.shop.system.util.ArrayPageList;
import com.our.shop.system.util.PageList;


@Repository(value = "baseDAO")
public class BaseDAO extends SqlSessionDaoSupport implements IBaseDAO {
	private static final Logger log = LoggerFactory.getLogger(CustomerController.class);
	@Override
	public List<HashMap<String, String>> select(String sql,Object obj) {
		return getSqlSession().selectList(sql, obj);
	}
	@Override
	public int insert(String sql,Object obj) {
		return getSqlSession().delete(sql, obj);
	}
	@Override
	public int update(String sql,Object obj) {
		return getSqlSession().update(sql, obj);
	}
	@Override
	public int delete(String sql,Object obj) {
		return getSqlSession().delete(sql, obj);
	}
	@Override
	/**
     * @param listStatementId 数据集 spring bean ID
     * @param parameterObject queryForList 参数
     * @param pageRequest   PageRequest 分页参数
     * @return
     */
    public PageList queryForPageList(String listStatementId, 
    		Object parameterObject, RowBounds pageRequest) {
    	log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>come in here>>>>>>>>>>>>>>>>>>>");
    	
		int skip = (pageRequest.getOffset() - 1) * pageRequest.getLimit();
		int max = pageRequest.getLimit();
		List<Object> list = getSqlSession().selectList(listStatementId, parameterObject, pageRequest);
		int total = 0;//!StringUtil.isEmpty(countStatementId)?Integer.parseInt(countStatementId):0;//queryForInt(countStatementId, parameterObject);
		log.debug(">>>>>>>>>>>>>>>>[page pagenition]>>>>>>>>>>>>>>>>>");
		if(null!=list
				&& 0!=list.size()){
			total = ((BaseObject)list.get(0)).getRowCount();
		}
		log.debug(">>>>>>>>[skip page]>>>>>"+skip);
		log.debug(">>>>>>>>[max page]>>>>>"+max);
		log.debug(">>>>>>>>>>>>>>>>>>[total record]>>>>>>>>>>>>>>>>>>>"+total);
		
		return new ArrayPageList(list, total, pageRequest);
	}
}
